﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Business;

public partial class admin_Mng_FaultsRecom : System.Web.UI.Page
{
    DataTable dt_result;
    DataTable dt_formtable;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadFaults();
        else
        {

            //    dt_result =(DataTable)  Session["dt"];
            //   GridView1.DataSource = dt_result;
            //  GridView1.DataBind();
        }
 
   
    }
    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //?????,?????? 
        GridView1.PageIndex = e.NewPageIndex;
        LoadFaults();
        GridView1.DataBind();
    }
    protected void TaskGridView_RowCreated(object sender,GridViewRowEventArgs e)
    {
      //  e.Row.Cells[5].Visible = false;
       // e.Row.Cells[6].Visible = false;
        //GV.Columns[5].Visible = false;//设置第6列为隐藏    
        //只需要在设置列为隐藏的地方多加一段代码：
        GridView1.DataKeyNames = new string[] { "fault_id", "recommend_id" };
        GridView1.Columns[5].Visible = false;
        GridView1.Columns[6].Visible = false;
     }
    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        GridView1.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        LoadFaults();
        //GridView1.DataBind();
    }
    protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        GridView1.EditIndex = -1;
        //Bind data to the GridView control.
        GridView1.DataBind();

        // BindData();
    }

    protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        //DataTable dt = (DataTable)Session["dt"];
        //){
        //}
        //Update the values.
        GridViewRow row = GridView1.Rows[e.RowIndex];
        string faulttitle = ((TextBox)(row.Cells[0].Controls[0])).Text;
        string faultdes = ((TextBox)(row.Cells[1].Controls[0])).Text;
        DropDownList severity = (DropDownList)row.Cells[2].FindControl("severity_dropdownlist");
        int status_id = Int32.Parse(severity.SelectedValue);
        string recom = ((TextBox)(row.Cells[3].Controls[0])).Text;
        int count = row.Cells.Count;
        int recom_id = Int32.Parse(GridView1.DataKeys[e.RowIndex]["recommend_id"].ToString());
      
        TextBox reco_label = (TextBox)row.Cells[5].Controls[0];
       string recoID = reco_label.Text;
       TextBox lb = (TextBox)row.Cells[6].Controls[0];
      //  int fault_id = Int32.Parse(((TextBox)(row.Cells[6].Controls[0])).Text);
       int fault_id = Int32.Parse(GridView1.DataKeys[e.RowIndex]["fault_id"].ToString());
        string query = "Update FaultsCondition set faultName='" + faulttitle + "',faultDes='"
            + faultdes + "' WHERE "
            + "fault_id='" + fault_id + "'";


        CreateConn objcon = new CreateConn();
        objcon.SQLConnDataTable(query);
        CreateConn objcon_reco = new CreateConn();
        string updateReco_query = "Update FaultRecommendation set status_id='" + status_id + "', recommendation='" + recom + "' WHERE "
           + "recommend_id='" + recom_id + "'";
        objcon_reco.SQLConnDataTable(updateReco_query);
        ////Reset the edit index.
        GridView1.EditIndex = -1;
        LoadFaults();
         GridView1.DataBind();
        ////Bind data to the GridView control.
        //BindData();
    }
    protected void LoadFaults()
    {
        string query = "SELECT faultName, faultDes,status_id,fc.fault_id ,recommendation,recommend_id"
            + " From FaultsCondition fc join FaultRecommendation fr on fc.fault_id"
            + "=fr.fault_id and fc.faultType!=0";
        CreateConn objconn = new CreateConn();
        try
        {
            dt_result = objconn.SQLConnDataTable(query);

            if (dt_result != null && dt_result.Rows.Count > 0)
            {
                //FormView1.DataSource = dt_result;
                // return;

                dt_result.Columns.Add("severity");
                foreach (DataRow dr in dt_result.Rows)
                {
                    string status = dr["status_id"].ToString();
                    dr.BeginEdit();
                    try
                    {

                        dr["severity"] = this.getsev(Int32.Parse(status));//change the value
                    }
                    catch (Exception e)
                    {
                        Response.Write("Err:" + e.ToString());
                    }
                    //  dt_result.Rows.
                    dr.EndEdit();
                    //  dr2.
                }
                //result_Enumerator = dt_result.Rows.GetEnumerator();

                // total_page = dt_result.Rows.Count / 10 + 1;
                //Session["TotalPage"] = total_page;
                Session["dt"] = dt_result;

                GridView1.DataSource = dt_result;
                GridView1.DataBind();

            }

        }
        catch (Exception e)
        {
            string errmsg = e.ToString();
        }

    }
    protected string getsev(int statusid)
    {
        string str = "";
        switch (statusid)
        {
            case 1: str = "NORMAL"; break;
            case 2: str = "ABNORMAL"; break;
            case 3: str = "Moderate"; break;
            case 4: str = "Severe"; break;
            default: str = ""; break;
        }
        return str;
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
        int faultID = Int32.Parse(DropDownList2.SelectedValue);

        int statusID = Int32.Parse(DropDownList3.SelectedValue);
        string recom = TextBox_recom.Text;

        string query = "INSERT INTO FaultRecommendation(fault_id,status_id,recommendation)"
            + "values('" + faultID + "','" + statusID + "','" + recom + "')";
        CreateConn objconn = new CreateConn();
        try
        {
            objconn.SQLConnDataTable(query);
            this.LoadFaults();
        }
        catch (Exception e2)
        {
            Response.Write(e2.ToString());
        }
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

}

